home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / HyperCard / BBS Folder / source / sendAECloseApp.c < prev    next >
C/C++ Source or Header  |  1991-08-11  |  3KB  |  122 lines

  1. /***
  2.  * sendAEOpen.c
  3.  *
  4.  *  This hypercard XFCN will send a AE open application command to some target
  5.  *  application.  The error returned will be returned to the caller.
  6.  *
  7.  *  Gordon Watts July '91 Copyright.
  8.  *
  9.  ***/
  10. #include    <HyperXCmd.h>
  11. #include "hyperUtil.h"
  12.  
  13. /**
  14.  ** some config defines...
  15.  **/
  16.  
  17. #define usageString "sendAECloseApp <program>"
  18. #define versionString "sendAECloseApp v1.0 Gordon Watts"
  19. #define badString "This should never be returned!  BuG! Bug!"
  20. #define badParamNumString "Wrong number of parameters.  Pass ? to see usage."
  21. #define badMake "Couldn't make the Apple Event!  Bad prog name?"
  22. #define badSend "Couldn't send the AE!"
  23. #define goodDone "\p"
  24. #define BugOut(error, string) {Str255 theString; \
  25.         done = true; \
  26.         paramPtr -> returnValue = strToHandle(paramPtr, string); \
  27.         NumToStr (paramPtr, error, theString); \
  28.         theString[theString[0]+1] = 0; \
  29.         SetGlobal (paramPtr, (StringPtr) "\ptheAEXError", \
  30.         (Handle) strToHandle (paramPtr, (char *) &(theString[1])));}
  31.  
  32. /**
  33.  * main 
  34.  *
  35.  *  Main routine for this xfunction.
  36.  *
  37.  **/
  38. pascal void main(XCmdPtr paramPtr)
  39. {
  40.     Boolean done = false;
  41.     TargetID theTarget;
  42.     register int theErr;
  43.     AEAddressDesc targetAddress;
  44.     AppleEvent theAppleEvent, theReplyEvent;
  45.  
  46.     /**
  47.      ** Set up default return values
  48.      **/
  49.  
  50.     paramPtr -> returnValue = strToHandle(paramPtr, badString);
  51.     paramPtr -> passFlag = false;
  52.     
  53.     /**
  54.      ** First, do the usage messages: a "!" and a "?".
  55.      **/
  56.     
  57.     if (paramPtr -> paramCount == 1) {
  58.     
  59.         switch (**(paramPtr -> params[0])) {
  60.         
  61.             case '!':
  62.                 BugOut (0, versionString);
  63.                 break;
  64.             
  65.             case '?':
  66.                 BugOut (0, usageString);
  67.                 break;
  68.             
  69.         }
  70.     }
  71.  
  72.     /**
  73.      ** Ok.  Next job, we had better make sure that we have the
  74.      ** correct number of parameters.  Since we need only the 
  75.      ** destination application, it had better be just one!
  76.      **/
  77.     
  78.     if ((!done) && (paramPtr -> paramCount != 1)) {
  79.         BugOut (0, badParamNumString);
  80.     }
  81.  
  82.     /**
  83.      ** Next job is to create the apple event with by using our little
  84.      ** home-made subprogram...
  85.      **/
  86.     
  87.     if (!done) {
  88.         Str255 theProgramName;
  89.         
  90.         ZeroToPas (paramPtr, (char *) (*(paramPtr->params[0])), theProgramName);
  91.         
  92.         theErr = AEFromHCProgram (theProgramName, kCoreEventClass,
  93.                 kAEQuitApplication, kAutoGenerateReturnID,
  94.                 kAnyTransactionID, &theAppleEvent);
  95.         
  96.         if (theErr != noErr) {
  97.             BugOut (theErr, badMake);
  98.         }
  99.     }
  100.  
  101.     /**
  102.      ** Finally, here we send the apple event...
  103.      **/
  104.     
  105.     if (!done) {
  106.         theErr = AESend (&theAppleEvent, &theReplyEvent,
  107.             kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  108.             kAENormalPriority, kAEDefaultTimeout, 0L, 0L);
  109.         if (theErr != noErr) {
  110.             BugOut (theErr, badSend);
  111.         }
  112.     }
  113.     
  114.     /**
  115.      ** If not done... We are finished...
  116.      **/
  117.     
  118.     if (!done) {
  119.         BugOut (0, goodDone);
  120.     }
  121. }
  122.